package fi.foyt.cs.persistence.dao.task;

import javax.persistence.EntityManager;

import fi.foyt.cs.persistence.dao.GenericDAO;
import fi.foyt.cs.persistence.domainmodel.task.TaskRewards;

public class TaskRewardsDAO extends GenericDAO<TaskRewards> {

  public TaskRewards create(Double money) {
    EntityManager entityManager = getEntityManager();

    entityManager.getTransaction().begin();
    try {
      TaskRewards taskRewards = new TaskRewards();
      taskRewards.setMoney(money);
      entityManager.persist(taskRewards);
      entityManager.getTransaction().commit();
      return taskRewards;
    } finally {
      if (entityManager.getTransaction().isActive())
        entityManager.getTransaction().rollback();
    }
  }
  
  public TaskRewards updateMoney(TaskRewards taskRewards, Double money) {
    EntityManager entityManager = getEntityManager();

    entityManager.getTransaction().begin();
    try {
      taskRewards.setMoney(money);
      taskRewards= entityManager.merge(taskRewards);
      entityManager.getTransaction().commit();
      return taskRewards;
    } finally {
      if (entityManager.getTransaction().isActive())
        entityManager.getTransaction().rollback();
    }
  }

}
